﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace Surveillance
{
    public class Elevator : Switchable
    {
        #region elevator declarations

        public static Texture2D elevatorTexture;
        private static readonly float MOVEMENT_SPEED = 0.5f;
        private int moveDirection;
        private float lowerLimit;
        private float upperLimit;
        private Rectangle targetLocation;
        private Rectangle initalRec;

        #endregion

        #region elevator constructors

        public Elevator(Rectangle targetLoc, float up, float down)
        {
            targetLocation = targetLoc;
            initalRec = targetLoc;
            upperLimit = up;
            lowerLimit = down;
            moveDirection = 0;
        }

        #endregion

        #region elevator public

        //mode:
        //0 - stop
        //-1 - up
        //1 - down
        public override void doSwitch(int mode)
        {

            moveDirection = mode;
        }

        #endregion

        #region eleveator update and draw

        public void Update(GameTime gameTime)
        {
            float elapsed = gameTime.ElapsedGameTime.Milliseconds;
            if (moveDirection != 0 && canMove())
            {
                targetLocation.Y += (int)(moveDirection * elapsed * MOVEMENT_SPEED);
            }

        }

        private bool canMove()
        {
            return ((moveDirection == 1 && targetLocation.Y < lowerLimit) || (moveDirection == -1 && targetLocation.Y > upperLimit));
        }

        public void Draw(SpriteBatch spriteBatch)
        {
             spriteBatch.Draw(
                elevatorTexture,
                targetLocation,
                null,
                Color.White,
                0.0f,
                Vector2.Zero,
                SpriteEffects.None,
                0.0f);
        }

        #endregion

        #region elevator properties

        public Rectangle TargetLocation
        {
            get { return targetLocation; }
            set { targetLocation = value; }
        }

        #endregion

        public void reset()
        {
            targetLocation = initalRec;
            moveDirection = 0;
        }
    }
}
